package com.lys.emsystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.mapper.CollegeMapper;
import com.lys.emsystem.entity.Major;
import com.lys.emsystem.service.MajorService;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.MajorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/major")
public class MajorController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private CollegeMapper collegeMapper;

    @PostMapping
    public Result search(@RequestBody MajorVo majorVo) {
        IPage<Major> page = majorService.search(new Page<>(majorVo.getPageIndex(), majorVo.getPageSize()),
                majorVo);
        majorVo.setMajorList(page.getRecords());
        majorVo.setTotalCount((int) page.getTotal());
        return Result.buildResult(Result.Status.OK, majorVo);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Major major) {
        int update = majorService.update(major);
        if (update > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Major major) {
        int add = majorService.add(major);
        if (add > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Major major) {
        int delete = majorService.delete(major.getId());
        if (delete > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result delList(@RequestBody MajorVo majorVo) {
        int i = majorService.deleteList(majorVo.getIds());
        if (i > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/find")
    public Result searchByCollegeId(@RequestBody() Major major) {
        List<Major> majors = majorService.searchByCollegeId(major.getId());
        return Result.buildResult(Result.Status.OK,majors);
    }


}
